package com.ruoyi.web.controller.system;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.GxComponentCategory;
import com.ruoyi.system.service.IGxComponentCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GxEvaluteData;
import com.ruoyi.system.service.IGxEvaluteDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评估表管理Controller
 *
 * @author ry
 * @date 2022-10-15
 */
@RestController
@RequestMapping("/system/evaluate")
public class GxEvaluteDataController extends BaseController
{
    @Autowired
    private IGxEvaluteDataService gxEvaluteDataService;

    @Autowired
    private IGxComponentCategoryService componentCategoryService;

    /**
     * 查询评估表管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GxEvaluteData gxEvaluteData)
    {
        startPage();
        List<GxEvaluteData> list = gxEvaluteDataService.selectGxEvaluteDataList(gxEvaluteData);
        return getDataTable(list);
    }

    /**
     * 获取评估菜单数据
     */
    @GetMapping("/menu")
    public AjaxResult menu()
    {

        AjaxResult ajax = AjaxResult.success();
        List<Map<String, Object>> menu = new ArrayList<>();

        //查询所有的类别
        List<GxEvaluteData> evaluateDataList = gxEvaluteDataService.selectGxEvaluteCategoryAll();
        List<GxComponentCategory> componentCategoryList = componentCategoryService.selectGxComponentCategoryAll();

        for (GxComponentCategory componentCategory: componentCategoryList) {
            Map<String, Object> categoryMap = new HashMap<>();
            categoryMap.put("id", UUID.randomUUID().toString());
            categoryMap.put("label", componentCategory.getName());
            List<Map<String, Object>> evaluateList = new ArrayList<>();
            for (GxEvaluteData evaluateData: evaluateDataList) {
                if (evaluateData.getCategory().equals(componentCategory.getId())) {
                    Map<String, Object> evaluateMap = new HashMap<>();
                    evaluateMap.put("id", evaluateData.getId());
                    evaluateMap.put("label", evaluateData.getTitle());
                    evaluateMap.put("name", evaluateData.getName());
                    evaluateList.add(evaluateMap);
                }
            }
            categoryMap.put("children", evaluateList);
            menu.add(categoryMap);
        }
        ajax.put("menu",menu);
        return ajax;
    }

    /**
     * 导出评估表管理列表
     */
    @Log(title = "评估表管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxEvaluteData gxEvaluteData)
    {
        List<GxEvaluteData> list = gxEvaluteDataService.selectGxEvaluteDataList(gxEvaluteData);
        ExcelUtil<GxEvaluteData> util = new ExcelUtil<GxEvaluteData>(GxEvaluteData.class);
        util.exportExcel(response, list, "评估表管理数据");
    }

    /**
     * 获取评估表管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gxEvaluteDataService.selectGxEvaluteDataById(id));
    }

    /**
     * 新增评估表管理
     */
    @Log(title = "评估表管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxEvaluteData gxEvaluteData)
    {
        return toAjax(gxEvaluteDataService.insertGxEvaluteData(gxEvaluteData));
    }

    /**
     * 修改评估表管理
     */
    @Log(title = "评估表管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxEvaluteData gxEvaluteData)
    {
        return toAjax(gxEvaluteDataService.updateGxEvaluteData(gxEvaluteData));
    }

    /**
     * 删除评估表管理
     */
    @Log(title = "评估表管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gxEvaluteDataService.deleteGxEvaluteDataByIds(ids));
    }
}
